package com.sanyou.task;

import com.sanyou.builders.CacheKeyBuilder;
import com.sanyou.enums.ModuleName;
import com.sanyou.mapper.CopperParticleMapper;
import com.sanyou.mapper.ProductMapper;
import com.sanyou.pojo.CopperParticleCheckRecord;
import com.sanyou.service.CacheService;
import com.sanyou.service.CopperParticleService;
import com.sanyou.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author xhy
 * @Description 铜粒子检测记录持久化到数据的定时任务
 */
@Component
public class CopperParticleRecordsPersistenceTask {
    private final long PAGE_SIZE =1000;
    Logger logger = LoggerFactory.getLogger(CopperParticleRecordsPersistenceTask.class);
    @Autowired
    private ProductService productService;

    @Autowired
    private CopperParticleService copperParticleService;

    /*@Scheduled(cron = "0 0 0 * * ?")
    public void persist(){
        int countOk=0;//计数器1
        int countError=0;//计数器2

        //获取前一天的年月日时间后缀
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,-1);
        String time = new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());


        //获取当前缓存中所有前一天的阴极铜表面缺陷检测记录模块的key
        String tempKey = new CacheKeyBuilder()
                .addSecondaryKey(ModuleName.COPPER_PARTICLE_RECORDS.getCode() +time)
                .addSecondaryKey("*")
                .build();
        Set<String> keys = cacheService.keys(tempKey);
        Set<CopperParticleCheckRecord> records = null;

        for (String key:keys) {
            //获取该机器下的记录数
            long size = cacheService.getSizeOfZSet(key);
            long page = size/PAGE_SIZE;
            //不停地取，持久到数据库，直到取完数据
            for(int i=0;i<=page;i++){
                records = cacheService.zRange(key,CopperParticleCheckRecord.class,i*PAGE_SIZE,(i+1)*PAGE_SIZE);
                if(records!=null && records.size()>0){
                    for (CopperParticleCheckRecord record:records) {
                        try {
                            copperParticleService.insertRecord(record);
                            countOk++;
                        } catch (Exception e) {
                            countError++;
                            logger.error("铜粒子检测记录持久化失败："+ record+"\n"+e.getMessage());
                        }
                    }
                }
            }

            cacheService.delete(key);
        }
        logger.debug(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"铜粒子检测记录持久化成功,本次成功持久化"+countOk+"条记录;失败"+countError+"条记录.");
    }*/

//    @Scheduled(cron = "0 0 0 * * ?")
//    public void persist2(){
//        //获取前一天的年月日时间
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DAY_OF_MONTH,-1);
//
//        //持久化前一天的缓存数据到数据库中
//        copperParticleService.persistDataOfYesterday(calendar.getTime());
//    }

}
